package net.larsan.dconf.cli;

import java.io.File;
import java.io.OutputStream;
import java.io.PrintWriter;

import com.google.inject.Singleton;

@Singleton
public class SystemOutputHandler implements OutputHandler {

	private PrintWriter mainWriter;
	private PrintWriter errWriter;
	
	@Override
	public OutputStream getDefaultOutput() {
		return System.out;
	}
	
	@Override
	public OutputStream getErrorOutput() {
		return System.err;
	}
	
	@Override
	public void printErr(String s, boolean ln) {
		if(errWriter == null) {
			errWriter = new PrintWriter(getErrorOutput());
		}
		if(ln) {
			errWriter.println(s);
		} else {
			errWriter.print(s);
		}
	}
	
	@Override
	public void printOut(String s, boolean ln) {
		if(mainWriter == null) {
			mainWriter = new PrintWriter(getDefaultOutput());
		}
		if(ln) {
			mainWriter.println(s);
		} else {
			mainWriter.print(s);
		}
	}

	@Override
	public File newFile(String name) {
		return new File(name);
	}

}
